package com.swxc.ssm.controller;


import com.swxc.ssm.po.Category;
import com.swxc.ssm.po.Course;
import com.swxc.ssm.service.CategoryService;
import com.swxc.ssm.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2017/7/28.
 */
@Controller
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private CourseService courseService;

    @RequestMapping("/deleteByPrimaryKey")
    public ModelAndView deleteByPrimaryKey(Integer ID){
        categoryService.deleteByPrimaryKey(ID);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("userShow");
        return modelAndView;
    }

    @RequestMapping("/insert")
    public ModelAndView insert(Category record){
        categoryService.insert(record);
        ModelAndView modelAndView = new ModelAndView();
        return modelAndView;
    }

    @RequestMapping("/insertSelective")
    public ModelAndView insertSelective(Category record){
        categoryService.insertSelective(record);
        ModelAndView modelAndView = new ModelAndView();
        return modelAndView;
    }

    @RequestMapping("/selectByPrimaryKey")
    public ModelAndView selectByPrimaryKey(@RequestParam(value="id", required = false) String id){

        ModelAndView modelAndView = new ModelAndView();
        if(Integer.parseInt(id) == 1){
            List<Course> courses = courseService.selectAll();
            modelAndView.addObject("courses",courses);

        }else{

            List<Course> courses1 = courseService.selectByCategoryId(Integer.parseInt(id));
            modelAndView.addObject("courses",courses1);
        }
        List<Category> categories = categoryService.selectAll();
        modelAndView.addObject("categorys",categories);

        List<String> list_class = new ArrayList<String>();
        list_class.add("iconfont mooc-quanbu");list_class.add("iconfont mooc-jisuanji");list_class.add("iconfont mooc-icon");list_class.add("iconfont mooc-xinli");list_class.add("iconfont mooc-waiyu");
        list_class.add("iconfont mooc-wenxuezhexuelishi");list_class.add("iconfont mooc-yishu");list_class.add("iconfont mooc-xuegongduiwuguanli");list_class.add("iconfont mooc-lilun");list_class.add("iconfont mooc-shengmingkexue");list_class.add("iconfont mooc-shu");
        list_class.add("iconfont mooc-falv");list_class.add("iconfont mooc-jiaoyu");list_class.add("iconfont mooc-qita");
        modelAndView.addObject("list_class",list_class);

        modelAndView.setViewName("course-index");
        return modelAndView;
    }

    @RequestMapping("/updateByPrimaryKeySelective")
    public ModelAndView updateByPrimaryKeySelective(Category record){
        categoryService.updateByPrimaryKeySelective(record);
        ModelAndView modelAndView = new ModelAndView();
        return modelAndView;
    }

    @RequestMapping("/updateByPrimaryKey")
    public ModelAndView updateByPrimaryKey(Category record){
        categoryService.updateByPrimaryKey(record);
        ModelAndView modelAndView = new ModelAndView();
        return modelAndView;
    }
}
